package com.ai.utils;

import com.alibaba.fastjson.JSON;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * 远程服务调用工具类
 * @author wu.xiaofei15
 * @Description
 * @date 2019/8/7 17:13
 */
public class RemoteServerUtil {

    // protected static final Logger logger = LoggerFactory.getLogger(RemoteServerUtil.class);

    /**
     * 使用Http Post方式访问接口
     * @param urlStr 请求地址
     * @param params 业务报文字符串
     * @return 结果报文
     * @throws Exception 异常
     */
    public static String remotePostService(String urlStr, String params){
        String ret = "";
        try {
            URL url = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置可输入
            connection.setDoInput(true);
            // 设置该连接是可以输出的
            connection.setDoOutput(true);
            // 链接超时时间
            connection.setConnectTimeout(10000);
            // 读取超时时间
            connection.setReadTimeout(10000);
            // 设置请求方式
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream()));
            pw.write(params);
            pw.flush();
            pw.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line = "";
            while ((line = br.readLine()) != null) { // 读取数据
                ret = ret + line;
            }
            // 关闭流
            br.close();
            // 断开连接 释放资源
            connection.disconnect();
        } catch (Exception e) {
            ret = "";
            System.out.println("远程http服务POST请求，url:{"+urlStr+"} params:{"+params+"} 异常:{"+e.getMessage()+"}");
            System.out.println(e.getMessage()+":\n"+ e);
        }
        return ret;
    }

    public static void main(String[] args) {

//    	//1.获取token
//    	String channelCode = "107";
//    	String key = "ls81bsvgh3PIGJ8xS2wQPkc6znzPsTBa";// 分配的渠道key
//    	String timestamp = DateFormatUtils.formatDate("yyyyMMddHHmmss");
//    	String nonceStr=UUID.randomUUID().toString().replace("-", "");
//    	String sign = Encrypt.getSign(channelCode, key, timestamp, nonceStr);
//
//    	String postUrl = "http://136.25.33.192:30100/dt-gateway/dt-sms/dt/getAccessToken";
//    	String json = "{\"channelCode\": \""+channelCode+"\",\"timestamp\": \""+timestamp+"\",\"nonceStr\": \""+nonceStr+"\",\"sign\": \""+sign+"\"}";
//
//    	System.out.println("postUrl: "+postUrl);
//    	System.out.println("json: "+json);
//
//    //	String accessToken = "7f14fb979399465197474f2a8c5c5dfb";
//
//    	//System.out.println(remotePostService(postUrl, json));
//
//    	String accessToken = "2789788882674a0690655e96c7589765";
//    	String resultData = remotePostService(postUrl, json);
//    	JSONObject jsonData = JSONObject.parseObject(resultData);
//    	 if (!jsonData.getBoolean("success")) {
//             System.out.println("调用token失败------");
//         }else {
//        	 accessToken = jsonData.getString("accessToken");
//         }
//
//    	 System.out.println("accessToken------"+accessToken);
//

        //2.通知短渠发送短信
        HashMap m = new HashMap();
        m.put("accessToken", "2789788882674a0690655e96c7589765");
        m.put("sendNbr", "10001");
        m.put("systemId", "107001");
        m.put("batchNumber", "2020072408");
        m.put("filePath", "dfs://ftpapp/sQzo/107001 telecom_sms_107001_2020072408_001.dat");
        m.put("totalNumber", "4");

        String sendJson = JSON.toJSONString(m);
        String sendUrl = "http://136.25.33.192:30100/dt-gateway/dt-sms/dtTemplate/sendGroupSmsTemplateMsgFile";
        System.out.println("sendJson---:"+sendJson);
        System.out.println(remotePostService(sendUrl, sendJson));


    }

}
